#include "stop_watch.h"

using namespace std;

#ifdef MSVC

static inline double GetFrequency() {
    LARGE_INTEGER freq;
    QueryPerformanceFrequency(&freq);
    return static_cast<double>(freq.QuadPart);
}

static inline double GetUSecTime() {
    LARGE_INTEGER count;
    QueryPerformanceCounter(&count);
    return static_cast<double>(count.QuadPart);
}

#else

static inline double GetFrequency() {
    return 1;
}

static inline double GetUSecTime() {
    timeval tv;
    gettimeofday(&tv, NULL);
    return static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) / 1000000.0;
}

#endif

double StopWatch::mFrequency = GetFrequency();

StopWatch::StopWatch() {
    Reset();
}

StopWatch::~StopWatch() {
}

void StopWatch::Reset() {
    mBeginTime = GetUSecTime();
}

double StopWatch::Elapsed() const {
    return (GetUSecTime() - mBeginTime) / mFrequency;
}

int64_t StopWatch::ElapsedSecond() const {
    return static_cast<int64_t>((GetUSecTime() - mBeginTime) / mFrequency);
}

int64_t StopWatch::ElapsedMilli() const {
    return static_cast<int64_t>((GetUSecTime() - mBeginTime) * 1000 / mFrequency);
}
